package com.forum.module.blog.controller.category;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.article.vo.ArticlePageReqVO;
import com.forum.module.blog.controller.article.vo.ArticleRespVO;
import com.forum.module.blog.controller.article.vo.ArticleSaveReqVO;
import com.forum.module.blog.controller.category.vo.CategoryPageReqVO;
import com.forum.module.blog.controller.category.vo.CategoryRespVO;
import com.forum.module.blog.controller.category.vo.CategorySaveReqVO;
import com.forum.module.blog.convert.article.ArticleConvert;
import com.forum.module.blog.convert.category.CategoryConvert;
import com.forum.module.blog.dal.dataobject.article.ArticleDO;
import com.forum.module.blog.dal.dataobject.category.CategoryDO;
import com.forum.module.blog.service.category.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/3 19:54
 */
@Tag(name = "博客管理 - 分类")
@RestController
@RequestMapping("/blog/category")
public class CategoryController {
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增分类")
    public DataResult<Long> createCategory(@RequestBody CategorySaveReqVO reqVO) {
        Long id = categoryService.createCategory(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新分类")
    public DataResult<Boolean> updateCategory(@RequestBody CategorySaveReqVO reqVO) {
        categoryService.updateCategory(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分类信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询分类列表")
    public DataResult<PageResult<CategoryRespVO>> page(@RequestBody @Valid CategoryPageReqVO pageReqVO) {
        // 获得文章分页列表
        PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(CategoryConvert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }


}
